package com.hd.assistant.web.common.base;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.citrus.turbine.Context;
import com.alibaba.citrus.turbine.Navigator;
import com.alibaba.fastjson.JSONObject;
import com.hd.assistant.biz.manager.AssistantManager;
import com.hd.assistant.biz.manager.exception.ManagerException;
import com.hd.assistant.biz.service.PaiPaiIsvService;
import com.hd.assistant.biz.service.exception.ServiceException;
import com.hd.assistant.biz.util.PaipaiUtil;
import com.hd.assistant.dal.dataobject.ClickedDetail;
import com.hd.assistant.dal.dataobject.User;
import com.hd.assistant.web.common.util.ClickedUtil;
import com.hd.common.taobao.DefaultTaobaoISVAppManager;



public class BaseModule {
	public static String APP_TYPE= "appType";
	public static String APP_FROM_PAIPAI = User.Type_PAIPAI;
	public static String APP_FROM_TAOBAO= User.Type_TAOBAO;
	
	@Autowired
	protected AssistantManager assistantManager;
	
	@Autowired
	protected PaiPaiIsvService paiPaiIsvService;
	
	@Autowired
	protected  DefaultTaobaoISVAppManager taobaoISVAppManager ;
	protected User checkLogin(HttpSession session,String currentApp,String type){
		if(type.equals(User.Type_PAIPAI)){
			Object o = session.getAttribute(PaipaiUtil.cookieMap.get(currentApp));
			if( o != null){
				return (User)o;
			}else{
				return null;
			}
		}else if(type.equals(User.Type_TAOBAO)){
			
		}
		return null;
	}

	protected void supplyUserInfo(User u,Context context) throws ServiceException{
		JSONObject object = paiPaiIsvService.loadUserInfo(u);
		u.setUserNick(object.getString("nickName"));
		context.put("userInfo", object);
	
}

	
	protected User getUserInfo(Long uid,String type) throws ManagerException {
		return assistantManager.getUserByUserIdAndType(uid, type);
	}
	
	protected void storeUserInfo(User u) throws ManagerException {
			assistantManager.insertUser(u);
		
	}
	
	protected void updateUserInfo(User u) throws ManagerException {
			assistantManager.updateUser(u);
	}
	
	public String getAppType(HttpSession session){
		//return (String)session.getAttribute(APP_TYPE);
		return User.Type_PAIPAI;
	}
	protected User checkPermision(String appType, HttpSession session,Context context,Navigator nav) {
    	if(appType == null)
    		appType = getAppType(session);
    	context.put(APP_TYPE,appType);
    	
    	if(appType!=null && appType.equals(APP_FROM_PAIPAI)){
        	User user = checkLogin(session,PaipaiUtil.APP_OAUTH_ID_ASSISTANT,User.Type_PAIPAI);
        	if(user == null){
        		nav.redirectToLocation(PaipaiUtil.getRedirectLoginUrl(PaipaiUtil.APP_OAUTH_ID_ASSISTANT));
        	}else{
        		context.put("user", user);
        		return user;
        	}
        	
    	}
    	return null;
	}
	
	protected void convertClickedIdToDb(Long userId, String clickedId,String title) throws ManagerException {
		ClickedDetail old = this.assistantManager.getNearestClickedDetail(clickedId);
		String today = new SimpleDateFormat("yyyyMMdd").format(new Date());
		if(old == null){
			//表示之前没有分享过
			ClickedDetail newDetail = new ClickedDetail();
			newDetail.setDataType(ClickedDetail.DATA_TYPE_USER);
			newDetail.setClickedDate(today);
			newDetail.setClickedTitle(title);
			newDetail.setClickedId(clickedId);
			newDetail.setItemId(ClickedUtil.getItemIdByclickedId(clickedId));
			newDetail.setUserId(userId+"");
			this.assistantManager.insertClickedDetail(newDetail);
		}else{
			//表示之前有分享记录
			if(today.equals(old.getClickedDate())){
				//不要处理 表示数据库里已经有记录
			}else{
				ClickedDetail newDetail = old.copy();
				newDetail.setId(null);
				newDetail.setClickedNum(0);
				newDetail.setDataType(ClickedDetail.DATA_TYPE_USER);
				newDetail.setClickedDate(today);
				newDetail.setClickedTitle(title);
				this.assistantManager.insertClickedDetail(newDetail);
			}
		
		}
		
	}

}
